package net.huashitong.Project.service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.service.SysTableService;
import com.sirdc.modules.sys.util.UserUtils;
import net.huashitong.Project.dao.RelocatePlanDao;
import net.huashitong.Project.entity.RelocatePlan;
import net.huashitong.Project.filter.RelocatePlanFilter;
import net.huashitong.user.entity.BnpUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RelocatePlanService extends StringPKService<RelocatePlan> {

    @Autowired
    private RelocatePlanDao dao;
    @Autowired
    private SysTableService tableService;

    @Override
    protected BaseDao<RelocatePlan, String> getDao() {
        return dao;
    }
    @Override
    protected void beforeSave(RelocatePlan obj) {
        obj.setSysId(tableService.updateMaxSysId("t_bnp_relocate_plan", null));
    }

    public List<RelocatePlan> queryByProjectId(String projectId){
        return dao.queryByProjectId(projectId);
    }

    //添加征迁计划
    public void addRelocatePlan(RelocatePlanFilter filter, BnpUser bnpUser){
        RelocatePlanFilter f = new RelocatePlanFilter();
        f.setDataTime(filter.getDataTime());
        f.setProjectId(filter.getProjectId());
        List<RelocatePlan> list = dao.queryRelocatePlansByFilter(f);
        if (list.size() > 0) {
            //如果有该时间的数据则更新，否则新增
            RelocatePlan relocatePlan = list.get(0);
            filter.setSysId(relocatePlan.getSysId());
            BeanUtils.copyProperties(filter, relocatePlan);
            this.onPreUpdate(relocatePlan);
            this.beforeUpdate(relocatePlan);
            relocatePlan.setUpdUser(bnpUser.getSysId());
            this.updateObject(relocatePlan);
            this.afterUpdate(relocatePlan);
        }else{
            RelocatePlan relocatePlan = new RelocatePlan();
            BeanUtils.copyProperties(filter, relocatePlan);
            String year = filter.getDataTime().substring(0,4);
            String month = filter.getDataTime().substring(5,7);
            relocatePlan.setRelocateYear(year);
            relocatePlan.setRelocateMonth(month);
            relocatePlan.setDataUnit("亩");
            this.onPrePersist(relocatePlan);
            this.beforeSave(relocatePlan);
            relocatePlan.setCreateUser(bnpUser.getSysId());
            this.saveObject(relocatePlan);
            this.afterSave(relocatePlan);
        }
    }
}
